<?php
/** 
 * @package AMFramework
 * @subpackage Data_Pack
 * @author Marcu Alexandru
 * @version 1.0 $Build 305b  
 */
defined('_INDEX') or die('Restricted');

abstract class Amfw_Database extends Amfw_Object 
{
	protected $_config 	= null;		// Configuratia bazei de date din aplicatia curenta
	protected $_query 	= null;		// Cererea, filtrata rezultata dupa setQuery
	protected $_linkId 	= null;		// Resursa catre conexiunea cu baza de date existenta
	protected $_cursor	= null;

	// Public =================================================================	
	public function setQuery($sql, $exec_query = false) {
		// Cererea este sanitizata si stocata ca proprietate a obiectului
		$this->_query = $this->_sanitizeSQL($sql);
		if($exec_query) { $this->query(); }
	}

	public function getQuery(){
		return $this->_query;
	}
	
	// Metode abstracte ce trebuie implementate in obiectele descendente
	abstract public function escape($input);
	abstract public function query();
	abstract public function getRow();
	abstract public function getRowList();
	abstract public function getAssoc();
	abstract public function getAssocList();
	abstract public function getResult();
	abstract public function getResultList();
	abstract public function getObject();
	abstract public function getObjectList();
	abstract public function getNumRows();
	abstract public function getCursor();
	abstract public function freeCursor();

	// Protected ==============================================================
	protected function _initConfig() {
		// Se citesc detaliile aplicatiei curente
		// si sunt stocate ca proprietate in obiect
		$app = Amfw_Application::_();
		$this->_config = $app->database;
	}
	
	// Private ================================================================
	private function _sanitizeSQL($sql) {
		// Metoda pentru sanitizarea cererii; nu include insa si 
		// sanitizarea informatiilor introduse de catre utilizator
		if(!is_string($sql))
			return null;

		// URL decode
		$sql = html_entity_decode($sql, ENT_QUOTES, "ISO-8859-1");

		// Decimal  and hexadecimal conversion
		$sql = preg_replace('/&#(\d+);/me',"chr(\\1)", $sql);
		$sql = preg_replace('/&#x([a-f0-9]+);/mei',"chr(0x\\1)", $sql);

		if (get_magic_quotes_gpc())
			$sql = stripslashes($sql);
		
		return $sql;
	}
}//~!Amfw_Database